package yxy.game.pm2.command.hitplanegame;

import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.planefight;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.ActiveModule_国庆寻宝;
import yxy.game.pm2.active.module.ActiveModule_心愿瓶;
import yxy.game.pm2.active.module.actcontrol.activeControl;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.PlayerBean;
import yxy.game.pm2.bean.ServerBean;
import yxy.game.pm2.bean.currency.Currencies;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.HitPlaneMessage;
import yxy.game.pm2.message.PackageMessage;
import yxy.game.pm2.module.CurrencyHandle;
import yxy.game.pm2.module.hitplanegame.HitPlaneGameModule;

import java.util.Collection;
import java.util.Map;


/****************************************
 * @name CMD_HitPlaneInfo
 * @description 打飞机页面信息
 * @author 梁健彬
 * @since 2022-09-05
 ***************************************/
@MessageType(proto = Command.HitPlaneInfo, classtype = HitPlaneMessage.HitPlaneInfoRequest.class)
public class CMD_HitPlaneInfo extends Command<HitPlaneMessage.HitPlaneInfoRequest> {

    @Override
    public void execute(MessageSource messageSource, HitPlaneMessage.HitPlaneInfoRequest request) throws Exception {
        String playerCode = messageSource.getTag();
        HitPlaneMessage.HitPlaneInfoResponse.Builder response = HitPlaneMessage.HitPlaneInfoResponse.newBuilder();
        try {
            HitPlaneGameModule module = HitPlaneGameModule.get(playerCode);
            if (module == null) {
                send(messageSource, response, Result.DataError);
                return;
            }
            PlayerBean pb = PlayerBean.get(playerCode);
            ServerBean sb = ServerBean.get(pb.getServerId());
            activeControl sai_心愿瓶 = activeControl.get(String.valueOf(sb.getAreaId()), String.valueOf(ActiveModule_心愿瓶.acttype));
            activeControl sai_国庆寻宝 = activeControl.get(String.valueOf(sb.getAreaId()), String.valueOf(ActiveModule_国庆寻宝.acttype));
            if (sai_心愿瓶.isRuning()) {
                ActiveModule_心愿瓶 am = ActiveModule_心愿瓶.get(playerCode, false);
                if (am == null) {
                    send(messageSource, response, Result.ActNoOpen);
                    return;
                }
                if (!am.running()) {
                    send(messageSource, response, Result.ActNoOpen);
                    return;
                }
                response.setDay(am.getDay());
                Map<String, planefight> allData = DataCenter.getAllData(planefight.class);

                for (planefight data : allData.values()) {
                    HitPlaneMessage.HitPlaneRewardMsg.Builder reward = HitPlaneMessage.HitPlaneRewardMsg.newBuilder();
                    reward.setId(data.getId());
                    Currencies curs = new Currencies(Currency.create(data.getRewardtype(), data.getRewardid(), data.getRewardnum()));
                    Collection<PackageMessage.Item> items = new CurrencyHandle().packItems(curs);
                    reward.addAllItem(items);
                    Boolean isReward = module.getRewardList().contains(data.getId()) ? true : false;
                    reward.setIsReward(isReward);
                    response.addReward(reward);
                }
            } else if (sai_国庆寻宝.isRuning()) {
                ActiveModule_国庆寻宝 am = ActiveModule_国庆寻宝.get(playerCode, false);
                if (am == null) {
                    send(messageSource, response, Result.ActNoOpen);
                    return;
                }
                if (!am.running()) {
                    send(messageSource, response, Result.ActNoOpen);
                    return;
                }
                response.setDay(am.getDay());
                Map<String, planefight> allData = DataCenter.getAllData(planefight.class);

                for (planefight data : allData.values()) {
                    HitPlaneMessage.HitPlaneRewardMsg.Builder reward = HitPlaneMessage.HitPlaneRewardMsg.newBuilder();
                    reward.setId(data.getId());
                    Currencies curs = new Currencies(Currency.create(data.getRewardtype2(), data.getRewardid2(), data.getRewardnum2()));
                    Collection<PackageMessage.Item> items = new CurrencyHandle().packItems(curs);
                    reward.addAllItem(items);
                    Boolean isReward = module.getRewardList().contains(data.getId()) ? true : false;
                    reward.setIsReward(isReward);
                    response.addReward(reward);
                }
            } else {
                send(messageSource, response, Result.DataError);
                return;
            }
            response.setCurrentId(module.getCurrentId());
            response.setChallengeNum(module.getTimes());

            send(messageSource, response, Result.Success);
        } catch (Exception e) {
            e.printStackTrace();
            send(messageSource, response, Result.DataError);
        }
    }

    private void send(MessageSource messageSource, HitPlaneMessage.HitPlaneInfoResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }


}
